<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field">
            <legend>系统菜单</legend>
            <div class="layui-field-box">
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal" id='addRootMenu'>添加根菜单</a>
            </div>
            </div>
        </fieldset>
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加子菜单</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../js/common/constant.js"></script>
<script>
    layui.use(['table', 'treetable','layer'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var layer = layui.layer;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 1,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: getRootPath()+'sysmenu/menus',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 100, title: '菜单名称'},
                {field: 'permission', title: '权限标识'},
                // {field: 'menuUrl', title: '菜单url'},
                // {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                // {
                //     field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                //         if (d.isMenu == 1) {
                //             return '<span class="layui-badge layui-bg-gray">按钮</span>';
                //         }
                //         if (d.parentId == -1) {
                //             return '<span class="layui-badge layui-bg-blue">目录</span>';
                //         } else {
                //             return '<span class="layui-badge-rim">菜单</span>';
                //         }
                //     }, title: '类型'
                // },
                {
                    field: 'icon', title: '图标', width: 80, align: 'center', templet: function (d) {
                        return '<i class="' + d.icon + '"></i>';
                    }
                },
                {field: 'href', minWidth: 100, title: '菜单url'},
                {field: 'sort', width: 60, align: 'center', title: '排序'},
                // {
                //     filed: 'flag', width: 80, align: 'center', title: '状态', templet: function (d) {
                //         if (d.flag == 0)
                //             return "使用中";
                //         else if (d.flag == 1)
                //             return "已禁用";
                //     }
                // },
                // {field: 'remark', minWidth: 100, title: '备注'},
                {templet: '#auth-state', width: 250, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });
        
        $("#addRootMenu").click(function(){
            var index = layer.open({
                    title: '添加系统菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '95%'],
                    content: '/page/menu/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            
        })

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    $.ajax({
                        url:getRootPath()+'sysmenu/delete',
                        type:"DELETE",
                        data:{
                            id:data.id 
                        },
                        dataType:"json",
                        success:function(data){
                            var index = layer.alert(data.message, {
                                title: '提示信息',
                            },function () {
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                if(data.code==200){
                                    obj.del();
                                }
                            })
                        }
                    }) 
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '添加系统菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '95%'],
                    content: '/page/menu/edit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (layEvent === 'add'){
                var index = layer.open({
                    title: '添加系统菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '95%'],
                    content: '/page/menu/add.html?pid='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>